var client = require("../DAOImpl/mongodb");
var ObjectID = require('mongodb').ObjectID;
var dataConfig = require("../config/database.json");
var host = dataConfig.host;
var database = dataConfig.db;
var db = new client(host, database);
var status = require("../config/status.json");

/*
 * @path /cuslogin
 * @req.body:
 *  kind the kind of the user, c(customer) or d(driver)
 *  cus_phone the phone number of the user
 *  cus_pwd  the password of the user
 */
function logIn(req, res, next) {
	console.log("login");
	var query = {'cus_phone':req.body.cus_phone ,'cus_pwd': req.body.cus_pwd};	
	
	if (req.body.kind === 'c'){
		var col = dataConfig.col_customer;
		db.findOne(col, query, function(err1, data){
			if(err1 ){
				var result = {"status": status.SYSTEMERROR.VALUE};
				res.send(result);
			}else{
				if(data == null){
					var result = {"status": status.LOGINFAILED.VALUE};
					res.send(result);
				}else{
				var result = {"status": status.LOGINSUCCESS.VALUE};
				result.user_id = data._id;
				res.send(result);
				}
			}
		});
	}else if(req.body.kind ==='d'){
		var col = dataConfig.col_driver;
		db.findOne(col, query, function(err1, data){
			if(err1 ){
				var result = {"status": status.LOGINFAILED.VALUE};
				res.send(result);
			}else{
				if(data == null){
					var result = {"status": status.LOGINFAILED.VALUE};
					res.send(result);
				}else{
					var result = {"status": status.LOGINSUCCESS.VALUE};
					result.user_id = data._id;
					res.send(result);
				}
				
			}
		});
	}else {
		res.send({"status": status.SYSTEMERROR.VALUE});
	}
	return next();
}

function logOff(req, res, next) {
	console.log("logoff");
	console.log(req.body.latitude + req.body.longitude);
	res.send({
		result : 'This is post ' + req.body.latitude +'   ' + req.body.longitude
	});
	return next();
}

/*
 *  @path /register
 *  @req.body:
 *  kind the kind of the user, c(customer) or d(driver)
 *  cus_phone the phone number of the user
 *  cus_pwd  the password of the user
 *  sex the sex of the user
 *  nickname the nickname of the user
 * {'kind':'c/d','cus_phone': '15824511223�'cus_password':�23456�'sex':'1/0', 'nick_name':'stuipy'}
 */
function register(req, res, next){
	
	var info = {
			"cus_phone": req.body.cus_phone,
			"cus_pwd": req.body.cus_pwd,
			"sex": req.body.sex,
			"nickname":req.body.nickname
	};
	if (req.body.kind === 'c'){
		var col = dataConfig.col_customer;
		console.log(col);
		db.insertIndex( col, info, 'cus_phone', function(err1, doc){
			if(err1){
				var result = {
						"status": status.USEREXIST.VALUE,
				}
				res.send(result);
			}else{
				var result = {
						"status": status.SUCCESS.VALUE,
				}
				result._id = doc[0]._id;
				res.send(result);
			}
		});		
	}else if(req.body.kind ==='d'){
		var col = dataConfig.col_driver;
		db.insertIndex(col, info, 'cus_phone',function(err1, doc){
			if(err1){
				var result = {
						"status": status.USEREXIST.VALUE,
				}
				res.send(result);
			}else{
				var result = {
						"status": status.SUCCESS.VALUE,
				}
				result._id = doc[0]._id;
				res.send(result);
			}
		});
	}else {
		res.send({"status": status.SYSTEMERROR.VALUE});
	}
	return next();
}
/*
 * req.body:
 * kind: c/d
 * "_id": req.body._id;
 * "cus_phone": req.body.cus_phone,
 * "cus_pwd": req.body.cus_pwd,
 * "sex": req.body.sex,
 * "nickname":req.body.nickname
 * 
 * "updateParam1", "updateParam2".......
 */
function Update(req, res, next){
	console.log("update");
	var result = new Object();
	var document = new Object();
	var selector = new Object();
	var newVar = new Object();
	
	if(!req.body._id){
		res.send({"status": status.SYSTEMERROR.VALUE});
	}else{
		selector._id = new ObjectID(req.body.cus_id);
	}

	for(var index in req.body){
		if(index !== "kind" && index !== "_id"){
			newVar[index] = req.body[index];
		}	
	}
	
	document.$set = new Object();
	document.$set = newVar;;

	if(req.body.kind ==='c'){
		db.update(dataConfig.col_customer, selector, document, function(err, doc){
			if(err){
				res.send({"status": status.SYSTEMERROR.VALUE});
			}else{

				var result = {
						"status": status.SUCCESS.VALUE,
				}
				result.update = doc;
				res.send(result);
			}
		});
	}else if(req.body.kind ==='d'){
		db.update(dataConfig.col_driver, selector, document, function(err, doc){
			if(err){
				res.send({"status": status.SYSTEMERROR.VALUE});
			}else{
				var result = {
						"status": status.SUCCESS.VALUE,
				}
				result.update = doc;
				res.send(result);
			}
		});
	}else{
		res.send({"status": status.SYSTEMERROR.VALUE});
	}
	return next;
}

/*
 * @req.body kind(c/d) cus_phone
 */
function checkPhone(req, res, next){
	console.log("checkPhone");
	var result = new Object();
	var selector = new Object();
	
	selector.cus_phone = req.body.cus_phone;
	if(req.body.kind ==="c"){
		db.findOne(dataConfig.col_customer, selector, function(err, data){
			if(err){
				result.status = status.ERROR.VALUE;
				res.send(result);
			}else{
				if (data==null) {
					result.status = status.SUCCESS.VALUE;
					res.send(result);
				} else {
					result.status = status.USEREXIST.VALUE;
					res.send(result);
				}
			}
		});
	}else if(req.body.kind ==="d"){
		db.findOne(dataConfig.col_driver, selector, function(err, data){
			if(err){
				result.status = status.ERROR.VALUE;
				res.send(result);
			}else{
				if (data==null) {
					result.status = status.SUCCESS.VALUE;
					res.send(result);
				} else {
					result.status = status.USEREXIST.VALUE;
					res.send(result);
				}
			}
		});
	}else{
		result.status = status.ERROR.VALUE;
		res.send(result);
	}
	return next;
}

/*
 * req.body: 'kind':c/d, 'cus_id', 
 * res return: the user information
 */
function getInfo(req, res, next){
	console.log("getInfo");
	var result = new Object();
	var query = new Object();
	query._id = new ObjectID(req.body.cus_id);
	
	if (req.body.kind =='c') {
		db.findOne(dataConfig.col_customer, query, function(err, doc){
			if(err || doc){
				result.status = status.ERROR.VALUE;
				res.send(result);
			}else{
				result.status = status.SUCCESS.VALUE;
				result.person_info = doc;
				res.send(result);
			}
		});
	} else if (req.body.kind =='d'){
		db.findOne(dataConfig.col_driver, query, function(err, doc){
			if(err || doc){
				result.status = status.ERROR.VALUE;
				res.send(result);
			}else{
				result.status = status.SUCCESS.VALUE;
				result.person_info = doc;
				res.send(result);
			}
		});
	} else {
		result.status = status.ERROR.VALUE;
		res.send(result);
	}
	return next;
}



exports.services = [ logIn, logOff, register, checkPhone, Update, getInfo];
